package org.edu.modules.center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.TimeStampUtil;
import org.edu.modules.center.entity.VeTeachAsk;
import org.edu.modules.center.service.IVeTeachAskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"问题表"})
@RestController
@RequestMapping({"/center/veTeachAsk"})
public class VeTeachAskController
        extends BaseController<VeTeachAsk, IVeTeachAskService>
{
    private static final Logger log = LoggerFactory.getLogger(VeTeachAskController.class);
    @Autowired
    private IVeTeachAskService veTeachAskService;

    @AutoLog("问题表-分页查询")
    @ApiOperation(value="分页查询", notes="问题表-分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeTeachAsk veTeachAsk, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeTeachAsk> queryWrapper = QueryGenerator.initQueryWrapper(veTeachAsk, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veTeachAsk);
        IPage<Map<String, Object>> pageList = this.veTeachAskService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("问题表-我的提问分页查询")
    @ApiOperation(value="我的提问分页查询", notes="问题表-我的提问分页查询")
    @PostMapping({"/myAskList"})
    public Result<?> myAskList(VeTeachAsk veTeachAsk, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        if (veTeachAsk.getUserid().equals(Integer.valueOf(0)))
        {
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//            veTeachAsk.setUserid(sysUser.getUserid());
        }
        QueryWrapper<VeTeachAsk> queryWrapper = QueryGenerator.initQueryWrapper(veTeachAsk, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veTeachAsk);
        IPage<Map<String, Object>> pageList = this.veTeachAskService.myAskPageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("问题表-通过id查询")
    @ApiOperation(value="通过id查询", notes="问题表-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeTeachAsk veTeachAsk = (VeTeachAsk)this.veTeachAskService.getById(id);
        if (veTeachAsk == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veTeachAsk);
    }

    @AutoLog("问题表-新增信息")
    @ApiOperation(value="新增信息", notes="问题表-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeTeachAsk veTeachAsk)
    {
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        int userId = 0;
        if (sysUser != null) {
//            userId = sysUser.getUserid().intValue();
        }
        Integer createTime = Integer.valueOf(TimeStampUtil.getNowSecondStamp());
        veTeachAsk.setUserid(Integer.valueOf(userId));
        veTeachAsk.setCreatetime(createTime);
        this.veTeachAskService.save(veTeachAsk);
        return Result.OK("添加成功！");
    }

    @AutoLog("问题表-修改信息")
    @ApiOperation(value="修改信息", notes="问题表-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeTeachAsk veTeachAsk)
    {
        this.veTeachAskService.updateById(veTeachAsk);
        return Result.OK("编辑成功!");
    }

    @AutoLog("问题表-通过id删除")
    @ApiOperation(value="通过id删除", notes="问题表-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veTeachAskService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("问题表-批量删除")
    @ApiOperation(value="批量删除", notes="问题表-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veTeachAskService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value="显示列表", notes="问题表-显示列表")
    @PostMapping({"/pageList"})
    public Result<?> pageList()
    {
        List<VeTeachAsk> teachAsks = this.veTeachAskService.list();
        return Result.OK(teachAsks);
    }
}
